/***********************************************
// FileName: MatroxMil.h
// ClassName: MatroxMil
// Description: initialize and use Matrox's MIL
// Author: xwt
// Date: 2007-6-30
************************************************/

#pragma once

#include <Mil.h>

class MatroxMil
{
public:
	MatroxMil(void);
	MatroxMil(long nImageWidth,long nImageHeight);
	~MatroxMil(void);

	BOOL PutMonoVideoData(MIL_ID BufferID ,PVOID pData, DWORD DataLenth);
	void SetDisplayWindow(MIL_ID DisplayID,MIL_ID BufferID,HWND nhwnd);
	void UnSelectWindow(MIL_ID DisplayID){MdispSelect(DisplayID, M_NULL);};
	void SetOverlay();
	void ZoomDisplay(MIL_ID DisplayID,double ZoomFactor);
	void GetModelSize(LPSTR pModelPath,LONG *lSizeX,LONG *lSizeY);
	
	//BOOL AngleLocal(LPSTR pModelPath,DOUBLE *FindAngle);
	//BOOL ModelLocal(LPSTR pModelPath,BOOL bCalculate);
	BOOL AngleLocal(LPSTR pModelPath,int nAngleRang,DOUBLE *FindAngle);
	BOOL ModelLocal(LPSTR pModelPath,BOOL bCalculate,long lAccept);
	void SortData(int nLen,BOOL ifXY);
	void DrawCutLine(DOUBLE *pData1,DOUBLE *pData2,BOOL ifXY,int nLen);
	void CalAngle(DOUBLE dAllowDis,DOUBLE *FindAngle);
	void CalPixelSize();

	void SimulateLine(DOUBLE &k,DOUBLE &b,const CArray<DOUBLE,DOUBLE&> &Data_X,const CArray<DOUBLE,DOUBLE&> &Data_Y);

	void CalDistance(DOUBLE *pArray,int nLength,DOUBLE dSize,DOUBLE *dDistance,DOUBLE *dResult);
	void AdjustFinePlane(int nLen,DOUBLE *dAngle,BOOL bEnable,DOUBLE dOffset);
	void AdjustCenter(int nLen,DOUBLE dDistanceX,DOUBLE dDistanceY,DOUBLE *dPosX,DOUBLE *dPosY);

	void SaveImage(MIL_ID BufferID,LPSTR nFileName)
	{
		if(BufferID)
		{
			MbufSave(nFileName,BufferID);
		}
	};
	
	MIL_ID GetApplication(){return m_Application;};
	MIL_ID GetSystem(){return m_System;};
	MIL_ID GetDisplay(){return m_Display;};
	MIL_ID GetGrabBuffer(){return m_GrabBuffer;};
	MIL_ID GetDispBuffer(){return m_DispBuffer;};
	MIL_ID GetProcBuffer(){return m_ProcBuffer;};
	MIL_ID GetScrBuffer(){return m_ScrBuffer;};
	MIL_ID GetOverlay(){return m_OverlayImage;};

	MIL_ID GetSubDisplay(){return m_SubDisplay;};
	MIL_ID GetSubDispBuffer(){return m_SubDispBuffer;};
	MIL_ID GetSubOverlay(){return m_SubOverlay;};

	MIL_ID GetModelDisplay(){return m_ModelDisplay;};
	MIL_ID GetModel(){return m_Model;};
	MIL_ID GetResult(){return m_Result;};
	MIL_ID GetModelImage(){return m_ModelImage;};

	LONG g_TransparentColor;

	LONG   m_nFindCount;
	LONG   m_lModel[56];
	DOUBLE m_dTime;
	DOUBLE m_dPosX[56];
	DOUBLE m_dPosY[56];
	DOUBLE m_dScore[56];
	DOUBLE m_dAngle[56];

	DOUBLE m_dWaferSizeX;
	DOUBLE m_dWaferSizeY;
	DOUBLE m_dDistanceX;
	DOUBLE m_dDistanceY;
	DOUBLE m_dPixelSizeX;
	DOUBLE m_dPixelSizeY;
	
	CArray<DOUBLE,DOUBLE&> pAnglePointX;
	CArray<DOUBLE,DOUBLE&> pAnglePointY;

	BOOL m_bFindTarget;
	BOOL m_bRotateFind;
	BOOL m_bCenterType;
	
private:
	MIL_ID m_Application;
	MIL_ID m_System;
	MIL_ID m_Display;
	MIL_ID m_GrabBuffer;
	MIL_ID m_DispBuffer;
	MIL_ID m_ProcBuffer;
	MIL_ID m_ScrBuffer;
	MIL_ID m_OverlayImage;

	MIL_ID m_SubDisplay;
	MIL_ID m_SubDispBuffer;
	MIL_ID m_SubOverlay;

	MIL_ID m_ModelDisplay;
	MIL_ID m_Model;
	MIL_ID m_Result;
	MIL_ID m_ModelImage;

	void InitMil();
	void FreeMil();
	void AllocBuffer(long nImageWidth,long nImageHeight);
	void FreeBuffer();
};
